<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="页面类型" prop="pageType">
        <el-select v-model="queryParams.pageType" placeholder="请选择类型" clearable @change="change">
          <el-option
            v-for="dict in dict.type.page_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
      <el-col :span="24">
        <el-form-item label="自定义标题" prop="title">
            <el-input v-model="formData.title" type="textarea" :autosize="{minRows: 1, maxRows: 4}" clearable :style="{width: '100%'}">
            </el-input>
            如果不想用默认的文章标题，可以自定义title内容
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="关键词" prop="keywords">
            <el-input v-model="formData.keywords" type="textarea" :autosize="{minRows: 1, maxRows: 4}" clearable :style="{width: '100%'}">
            </el-input>
            多个关键词用英文逗号隔开，优先显示此处设置的关键词，未设置则使用标签
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="描述" prop="description">
          <el-input v-model="formData.description" type="textarea" :autosize="{minRows: 4, maxRows: 10}" :style="{width: '100%'}"></el-input>
            优先显示此处设置的描述，未设置则使用文章摘要，无摘要则截取手段内容
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item size="large">
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-col>
    </el-form>
  </div>
</template>


<script>
import { getSeo, saveSeo } from "@/api/content/seo";

export default {
  name: "Seo",
  dicts: ['page_type'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageType: undefined
      },
      formData: {
        pageType: '',
        title: '',
        keywords: '',
        description: '',
      },
      // 表单校验
      rules: {
        pageType: [
          { required: true, message: "请选择页面类型", trigger: "blur" }
        ],
        title: [
          { required: true, message: "页面title不能为空", trigger: "blur" }
        ],
        keywords: [
          { required: true, message: "页面keywords不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "页面description不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    getData() {
      this.loading = true;
      getSeo(this.queryParams.pageType).then(response => {
          if (response.data === undefined) {
            this.formData = {};
            this.$refs['elForm'].resetFields();
          } else {
            this.formData = response.data;
          }
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      let pageType = this.queryParams.pageType;
      if (pageType === undefined || pageType === '') {
        this.$modal.msgError('请选择页面类型再查询!');
        return;
      }
      this.getData();
    },
    /** 重置按钮操作 */
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if(valid) {
          // 提交表单
          let pageType = this.queryParams.pageType;
          if (pageType === undefined || pageType === '') {
            this.$modal.msgError('请选择页面类型再提交!');
            return;
          }
          this.formData.pageType = pageType;
          saveSeo(this.formData).then(() => {
            this.$modal.msgSuccess('保存成功');
          }).catch(() => {});
        }
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    change() {
      let pageType = this.queryParams.pageType;
      if (pageType === undefined || pageType === '') {
        this.$refs['elForm'].resetFields();
        return;
      }
      this.getData();
    }
  }
};
</script>